layui.use(['form', 'laydate', 'layer', 'element'], function () {
    // var $ = layui.jquery;
    // layer = layui.layer;
    // var element = layui.element;
    // var form = layui.form;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#test1'
    });
    laydate.render({
        elem: '#edu_start_time',
        type: 'month'
    })
    laydate.render({
        elem: '#edu_end_time',
        type: 'month'
    })
    laydate.render({
        elem: '#pra_start_time',
        type: 'month'
    })
    laydate.render({
        elem: '#pra_end_time',
        type: 'month'
    })
    laydate.render({
        elem: '#pro_start_time',
        type: 'month'
    })
    laydate.render({
        elem: '#pro_end_time',
        type: 'month'
    })
});

function setFormValues(el, data) { 
    for (var p in data) {
        if (p == "sex") {
            if (data[p] == 1) {
                $("input[name=sex][value='1']").attr('checked', true);
            } else {
                $("input[name=sex][value='0']").attr('checked', true);
            }
        } else {
            el.find(":input[name='" + p + "']").val(data[p]);
        }
    }
   // layui.form.render();
}

function getBasicInfo() {
    var data = {
        id: $.cookie('id')
    }
    $.ajax({
        url: "http://localhost:3000/info/basic ",
        type: 'post',
        data: data,
        dataType: 'json',
        success: function (result) {
            console.log(result);
            setFormValues($("#base"), result);
            $("[name=evaluate]").val(result.evaluate);
            $("[name=others]").val(result.others);
            $("[name=skills]").val(result.skills);
            $("#name").val(result.name);
            addResumeBase(result);
        //    layui.form.render();
        },
        error: function (msg) {

        }
    })
}

function getEducationInfor() {
    var data = {
        id: $.cookie('id')
    }
    $.ajax({
        url: "http://localhost:3000/info/edu ",
        type: 'post',
        data: data,
        dataType: 'json',
        success: function (result) {
            console.log(result);
            addResumeEdu(result);
            var diploma;
            for (var i = 0; i < result.edu.length; i++) {
                if (result.edu[i].diploma == 0) {
                    diploma = "本科";
                } else if (result.edu[i].diploma == 1) {
                    diploma = "硕士";
                } else {
                    diploma = "博士";
                }
                $("#education_all").append("<div class='layui-colla-item'>\
                    <h2 class='layui-colla-title'>" + diploma + "\
                    <button class = 'layui-btn layui-btn-sm layui-btn-warm' style='float:right' onclick='deleteEdu(this," + result.edu[i].id +
                    ")'><i class = 'layui-icon'> &#xe640;</i></button>\
                    </h2>\
                    <div class='layui-colla-content'>\
                    <p>" + result.edu[i].start_time.substring(0, 7) + "至" + result.edu[i].end_time.substring(0, 7) + "</p>\
                    <p>" + result.edu[i].education_exp + "</p>\
                   </div>\
                 </div>");
            }
        },
        error: function (msg) {

        }
    })
}

function getPracticeInfo() {
    var data = {
        id: $.cookie('id')
    }
    $.ajax({
        url: "http://localhost:3000/info/practice ",
        type: 'post',
        data: data,
        dataType: 'json',
        success: function (result) {
            addResumePra(result);
            for (var i = 0; i < result.pra.length; i++) {
                $("#practice_all").append("<div class='layui-colla-item'>\
                  <h2 class='layui-colla-title'>" + result.pra[i].company + "\
                  <button class = 'layui-btn layui-btn-sm layui-btn-warm' style='float:right' onclick='deletePra(this," + result.pra[i].id +
                    ")'><i class = 'layui-icon'> &#xe640;</i></button>\
                  </h2>\
                  <div class='layui-colla-content'>\
                  <p>" + result.pra[i].start_time.substring(0, 7) + "至" + result.pra[i].end_time.substring(0, 7) + "</p>\
                  <p>" + result.pra[i].practice_exp + "</p>\
                 </div>\
              </div>");
            }
        },
        error: function (msg) {

        }
    })
}

function getProjectInfo() {
    var data = {
        id: $.cookie('id')
    }
    $.ajax({
        url: "http://localhost:3000/info/project ",
        type: 'post',
        data: data,
        dataType: 'json',
        success: function (result) {
            addResumePro(result);
            console.log(result);
            for (var i = 0; i < result.pro.length; i++) {
                $("#project_all").append("<div class='layui-colla-item'>\
                    <h2 class='layui-colla-title'>" + result.pro[i].title + "\
                    <button class = 'layui-btn layui-btn-sm layui-btn-warm' style='float:right' onclick='deletePro(this," + result.pro[i].id +
                    ")'><i class = 'layui-icon'> &#xe640;</i></button>\
                    </h2>\
                    <div class='layui-colla-content'>\
                    <p>" + result.pro[i].start_time.substring(0, 7) + "至" + result.pro[i].end_time.substring(0, 7) + "</p>\
                    <p>" + result.pro[i].project_exp + "</p>\
                   </div>\
                 </div>");
            }
        },
        error: function (msg) {

        }
    })
}

function getHonerInfo() {
    var data = {
        id: $.cookie('id')
    }
    $.ajax({
        url: "http://localhost:3000/info/honor ",
        type: 'post',
        data: data,
        dataType: 'json',
        success: function (result) {
            console.log(result);
            addResumeHon(result);
            for (var i = 0; i < result.honor.length; i++) {
                $("#honor_all").append("<div class='layui-colla-item'>\
                    <h2 class='layui-colla-title'>" +result.honor[i].honor+"\
                    <button class = 'layui-btn layui-btn-sm layui-btn-warm' style='float:right' onclick='deleteHon(this," + result.honor[i].id +
                    ")'><i class = 'layui-icon'> &#xe640;</i></button>\
                    </h2>\
                 </div>");
            }
        },
        error: function (msg) {

        }
    })
}

function addResumeBase(result) {
    var sex;
    if (result.sex == 1) {
        sex = "女";
    } else {
        sex = "男";
    }
    var politic;
    switch (result.politic) {
        case 0:
            politic = "本科";
            break;
        case 1:
            politic = "硕士";
            break;
        case 2:
            politic = "博士";
        default:
            ;
    }
    var now = new Date().getFullYear();
    var birth = now - result.birthday.substring(0, 4);
    $("#sex").append(sex + "&nbsp;&nbsp;" + birth + "岁&nbsp;&nbsp;" + result.homeland + "&nbsp;&nbsp;" + politic);
    $("#phone").append(result.phone + "&nbsp;&nbsp;" + result.email);
    $("#pro_skill").append("<p style='text-indent:2em' class='font-sm'>"+result.skills+"</p>");
    $("#pro_evaluate").append("<p style='text-indent:2em' class='font-sm'>"+result.evaluate+"</p>");
}

function addResumeEdu(result) {
    for (var i = 0; i < result.edu.length; i++) {
        if (result.edu[i].diploma == 0) {
            diploma = "本科";
        } else if (result.edu[i].diploma == 1) {
            diploma = "硕士";
        } else {
            diploma = "博士";
        }
        $("#edu_resume").append("<p class='font-sm'><strong>" + result.edu[i].start_time.substring(0, 7) +
            "至" + result.edu[i].end_time.substring(0, 7) + "</strong>&nbsp;&nbsp" + diploma + "&nbsp;&nbsp" +
            result.edu[i].education_exp + "</p>");
    }
}

function addResumePra(result) {
    for (var i = 0; i < result.pra.length; i++) {
        $("#pra_resume").append("<p class='font-sm'><strong>" + result.pra[i].start_time.substring(0, 7) + "至" +
            result.pra[i].end_time.substring(0, 7) + "</strong>&nbsp;&nbsp;" + result.pra[i].company + "&nbsp;&nbsp;" +
            result.pra[i].practice_exp + "</p>");
    }
}

function addResumeHon(result){
    for (var i = 0; i < result.honor.length; i++) {
        $("#hon_resume").append("<p class='font-sm'>"+result.honor[i].honor+"</p>");
    }
}

function addResumePro(result){
    for (var i = 0; i < result.pro.length; i++) {
        $("#pro_resume").append("<p class='font-sm'><strong>"+result.pro[i].start_time.substring(0, 7) + 
        "至" + result.pro[i].end_time.substring(0, 7)+"</strong>&nbsp;&nbsp;"+result.pro[i].project_exp+"</p>");
    }
}